import 'package:flutter/material.dart';
import 'package:gzmall_app/common/color_common.dart';
import 'package:gzmall_app/common/net_util.dart';
import 'package:gzmall_app/common/api.dart';
import 'package:gzmall_app/common/toast_util.dart';
import 'package:gzmall_app/model/user.dart';

class ThirdAuthLoginPage extends StatefulWidget {
  String openId;

  ThirdAuthLoginPage(this.openId);

  @override
  State<StatefulWidget> createState() => ThirdAuthLoginPageState(this.openId);
}

class ThirdAuthLoginPageState extends State<ThirdAuthLoginPage> {
  String _phone, _password;
  String openId;
  TextEditingController _accountController, _pwdController;

  ThirdAuthLoginPageState(this.openId);

  @override
  void initState() {
    super.initState();
    _accountController = TextEditingController();
    _pwdController = TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: ListView(
        children: <Widget>[
          Column(
            children: <Widget>[
              Stack(
                alignment: const FractionalOffset(0.0, 0.5),
                children: <Widget>[
                  Container(
                    alignment: Alignment.center,
                    width: double.infinity,
                    height: 56.0,
                    child: Text(
                      "绑定手机号",
                      textAlign: TextAlign.center,
                      style: new TextStyle(
                          fontSize: 20.0, color: ColorCommon.black1),
                    ),
                  ),
                  InkWell(
                    child: Padding(
                      padding: const EdgeInsets.only(left: 15.0),
                      child: Icon(Icons.close),
                    ),
                    onTap: () {
                      Navigator.of(context).pop();
                    },
                  ),
                ],
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  new Padding(
                    padding: const EdgeInsets.only(
                        left: 50.0, right: 50.0, top: 50.0),
                    child: new Row(
                      children: <Widget>[
                        new Expanded(
                          child: new Text("国家",
                              style: new TextStyle(
                                  fontSize: 16.0, color: ColorCommon.black1)),
                        ),
                        new Text("+86",
                            style: new TextStyle(
                                fontSize: 16.0, color: ColorCommon.black1)),
                        new Padding(
                          padding: const EdgeInsets.only(left: 10.0),
                          child: new Image.asset("images/right_arrow.png",
                              width: 10.0, height: 10.0),
                        ),
                      ],
                    ),
                  ),
                  new Container(
                    height: 54.0,
                    padding: const EdgeInsets.only(left: 20.0, right: 20.0),
                    margin: const EdgeInsets.only(
                        left: 30.0, right: 30.0, top: 30.0, bottom: 20.0),
                    alignment: Alignment.center,
                    decoration: new BoxDecoration(
                        color: ColorCommon.grey2,
                        borderRadius: new BorderRadius.circular(100.0)),
                    child: new Row(
                      children: <Widget>[
                        new Padding(
                          padding: const EdgeInsets.only(right: 10.0),
                          child: new Icon(Icons.lock),
                        ),
                        new Expanded(
                          child: new TextField(
                            controller: _accountController,
                            style: new TextStyle(color: ColorCommon.black1),
                            decoration: new InputDecoration.collapsed(
                              hintText: "+86 请输入手机号",
                              hintStyle: TextStyle(
                                  fontSize: 12.0, color: ColorCommon.grey1),
                            ),
                            onChanged: (str) {
                              _phone = str ?? '';
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                  new Container(
                    height: 54.0,
                    padding: const EdgeInsets.only(left: 50.0, right: 20.0),
                    margin: const EdgeInsets.only(
                        left: 30.0, right: 30.0, bottom: 20.0),
                    alignment: Alignment.center,
                    decoration: new BoxDecoration(
                        color: ColorCommon.grey2,
                        borderRadius: new BorderRadius.circular(100.0)),
                    child: new Row(
                      children: <Widget>[
                        new Expanded(
                          child: new TextField(
                            controller: _pwdController,
                            style: new TextStyle(color: ColorCommon.black1),
                            decoration: new InputDecoration.collapsed(
                              hintText: "密码",
                              hintStyle: TextStyle(
                                  fontSize: 12.0, color: ColorCommon.grey1),
                            ),
                            onChanged: (str) {
                              _password = str ?? '';
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
              InkWell(
                onTap: () {
                  _loginAction();
                },
                child: Container(
                  height: 54.0,
                  margin: const EdgeInsets.fromLTRB(30.0, 50.0, 30.0, 50.0),
                  alignment: Alignment.center,
                  decoration: new BoxDecoration(
                      color: ColorCommon.primaryColor,
                      borderRadius: new BorderRadius.circular(100.0)),
                  child: Text(
                    "绑定",
                    style: new TextStyle(color: Colors.white),
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  _loginAction() {
    if (_phone == null || _phone.isEmpty) {
      ToastUtil.showMessageToast("请输入电话号码");
      return;
    }
    if (_password == null || _password.isEmpty) {
      ToastUtil.showMessageToast("请输入密码");
      return;
    }
    _thirdAuthLogin(this.openId, _phone, _password);
  }

  _thirdAuthLogin(String openId, String phone, String password) {
    Map<String, String> params = {
      "open_id": openId,
      "phone": phone,
      "password": password,
    };

    NetUtil.get(
      context,
      Api.userLoginWechat,
      (data, message) {
        ToastUtil.showMessageToast(message);
        User user = User.fromJson(data);
        User.saveUserToken(user.authToken);
        User.saveUserInfo(user);
        Navigator.of(context).pop(true);
      },
      params: params,
    );
  }
}
